package priv.mill.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import priv.mill.db.Service;
import priv.mill.entry.Questions;
import priv.mill.util.HandlerSet;
import priv.mill.util.JDBCUtil;

public class QuestionsByPageHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		
		try {
			List<Questions> all = JDBCUtil.query("select * from questions limit ?,?", new HandlerSet<Questions>() {
				/**
				 *  answer       | int(10)
 chapterId    | int(11)
 difficulty   | int(11)
 explain      | varchar(300)
 id           | int(11)
 label        | varchar(300)
 mediaHeight  | int(11)
 mediaType    | int(11)
 mediaWidth   | int(11)
 optionA      | varchar(300)
 optionB      | varchar(300)
 optionC      | varchar(300)
 optionD      | varchar(300)
 optionE      | varchar(300)
 optionF      | varchar(300)
 optionG      | varchar(300)
 optionH      | varchar(300)
 optionType   | int(11)
 question     | varchar(500)
 questionId   | varchar(20)
 mediaContent | varchar(300)
 falseCount   | int(11)
 trueCount    | int(11)
 wrongRate    | double(10,9)
				 */
				@Override
				public Questions handler(ResultSet rs) throws SQLException {
					Questions questions = new Questions();
					questions.setAnswer(rs.getInt("answer"));
					questions.setChapterid(rs.getInt("chapterId"));
					questions.setExplain(rs.getString("explain"));
					questions.setFalsecount(rs.getInt("falseCount"));
					questions.setId(rs.getInt("id"));
					questions.setLabel(rs.getString("label"));
					questions.setOptiona(rs.getString("optionA"));
					questions.setOptionb(rs.getString("optionB"));
					questions.setOptionc(rs.getString("optionC"));
					questions.setOptiond(rs.getString("optionD"));
					questions.setOptiontype(rs.getInt("optionType"));
					questions.setQuestion(rs.getString("question"));
					questions.setQuestionid(rs.getString("questionId"));
					questions.setMediacontent(rs.getString("mediaContent"));
					return questions;
				}
			}, 1,20);
			response.getWriter()
			.append(JSON.toJSONString(all));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
